//
//  MCDietHealthVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/5.
//

import UIKit
import SnapKit
import JXSegmentedView
import SwiftyJSON
import Toast_Swift
import QMUIKit
import MJRefresh
import Kingfisher

class MCDietHealthVc: MCBaseVc {
    
    let dietHealthIdentifier:String = "dietHealthIdentifier"
    
    
    var dietHealthCollectionV:UICollectionView?
    
    var count = 1
    
    var dataSource:Array = [DietRecord?]()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let dietHealthCollLayout = UICollectionViewFlowLayout.init()
        
        dietHealthCollectionV = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: dietHealthCollLayout)
        dietHealthCollectionV?.delegate = self
        dietHealthCollectionV?.dataSource = self
        dietHealthCollectionV?.backgroundColor = UIColor.white
        dietHealthCollectionV?.register(MCDietHealthCollectionCell.self, forCellWithReuseIdentifier: dietHealthIdentifier)
        view.addSubview(dietHealthCollectionV!)
        dietHealthCollectionV?.snp.makeConstraints { make in
            make.edges.equalTo(view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
       
        self.dietHealthCollectionV?.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.count = 1
            self.getDietHealthListData(current: self.count)
        })
        
        self.dietHealthCollectionV?.mj_header?.beginRefreshing()
        
        self.dietHealthCollectionV?.mj_footer = MJRefreshAutoFooter.init(refreshingBlock: {
            self.getDietHealthListData(current: self.count)
        })
        
        
    }
    
    func getDietHealthListData(current:Int?) -> Void{
        NetWorkRequest(MCMyCollectApi.myDietHealthList(current: current)) { ResponseModel in
            self.dietHealthCollectionV?.mj_header?.endRefreshing()
            self.dietHealthCollectionV?.mj_footer?.endRefreshing()
            
            let dietRecordModel = DietCollectList.init(JSONString: ResponseModel.dataString)
            
            if current == 1 {
                self.dataSource.removeAll()
                self.dataSource.append(contentsOf: dietRecordModel?.records ?? [])
                
            }else {
                self.count += 1
                self.dataSource.append(contentsOf: dietRecordModel?.records ?? [])
            }
            
            self.dietHealthCollectionV?.reloadData()
            
        } failureCallback: { ResponseModel in
            self.dietHealthCollectionV?.mj_header?.endRefreshing()
            self.dietHealthCollectionV?.mj_footer?.endRefreshing()
            self.view.makeToast("网络失败",position: .center)
        }
    }

    

}

extension MCDietHealthVc:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (UIScreen.main.bounds.size.width - 30) / 2, height: 140)
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCDietHealthCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: dietHealthIdentifier, for: indexPath) as! MCDietHealthCollectionCell
        
        let recordDietModel = self.dataSource[indexPath.row]
        
        cell.titleInfoLabel.text = recordDietModel?.zhName ?? ""
        cell.kcalInfoLabel.text = "≈ \(String(describing: recordDietModel?.calorie!)) Kcal"
        cell.icImageView.kf.setImage(with: URL(string: recordDietModel?.previewUrl ?? ""))
        
        if recordDietModel?.status == 0 {
            cell.collectionInfoBtn.setImage(UIImage(named: ""), for: .normal)
        }else {
            cell.collectionInfoBtn.setImage(UIImage(named: ""), for: .normal)
        }
        
       
        return cell
    }
    
}

extension MCDietHealthVc:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

